package com.todod.base.module.infra.api.file;

import cn.hutool.core.io.IoUtil;
import com.todod.base.framework.common.pojo.CommonResult;
import com.todod.base.module.infra.api.file.dto.FileCreateReqDTO;
import com.todod.base.module.infra.api.file.dto.FileRelationReqDTO;
import com.todod.base.module.infra.enums.ApiConstants;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.todod.base.framework.common.pojo.CommonResult.success;

@FeignClient(name = ApiConstants.NAME) // TODO ：fallbackFactory =
@Tag(name = "RPC 服务 - 文件")
public interface FileApi {

    String PREFIX = ApiConstants.PREFIX + "/file";

    /**
     * 保存文件，并返回文件的访问路径
     *
     * @param content 文件内容
     * @return 文件路径
     */
    default String createFile(byte[] content) {
        return createFile(null, null, content);
    }

    /**
     * 保存文件，并返回文件的访问路径
     *
     * @param path 文件路径
     * @param content 文件内容
     * @return 文件路径
     */
    default String createFile(String path, byte[] content) {
        return createFile(null, path, content);
    }

    /**
     * 保存文件，并返回文件的访问路径
     *
     * @param name 原文件名称
     * @param path 文件路径
     * @param content 文件内容
     * @return 文件路径
     */
    default String createFile(@RequestParam("name") String name,
                              @RequestParam("path") String path,
                              @RequestParam("content") byte[] content) {
        return createFile(new FileCreateReqDTO().setName(name).setPath(path).setContent(content)).getCheckedData();
    }

    @PostMapping(PREFIX + "/create")
    @Operation(summary = "保存文件，并返回文件的访问路径")
    CommonResult<String> createFile(@Valid @RequestBody FileCreateReqDTO createReqDTO);

    @GetMapping(PREFIX + "/get")
    @Operation(summary = "通过 ID 查询文件信息")
    @Parameter(name = "id", description = "id", example = "1", required = true)
    FileCreateReqDTO getFile(@RequestParam("id") Long id);

    default FileCreateReqDTO newCreateFileByte(@RequestParam("foreignTableId") Long foreignTableId,@RequestParam("foreignTableName") String foreignTableName, @RequestParam("foreignFileType") String foreignFileType, @RequestParam("name") String name, @RequestParam("path") String path, @RequestParam("content") byte[] content){
        return newCreateFileByte(new FileCreateReqDTO().setForeignTableId(foreignTableId).setForeignTableName(foreignTableName).setForeignFileType(foreignFileType).setName(name).setPath(path).setContent(content));
    };
    @PostMapping(PREFIX + "/uploadFileByte")
    FileCreateReqDTO newCreateFileByte(@Valid @RequestBody FileCreateReqDTO createReqDTO);

    default FileCreateReqDTO newUpdateFileByte(@RequestParam("foreignTableId") Long foreignTableId,@RequestParam("foreignTableName") String foreignTableName, @RequestParam("foreignFileType") String foreignFileType, @RequestParam("name") String name, @RequestParam("path") String path, @RequestParam("content") byte[] content){
        return newUpdateFileByte(new FileCreateReqDTO().setForeignTableId(foreignTableId).setForeignTableName(foreignTableName).setForeignFileType(foreignFileType).setName(name).setPath(path).setContent(content));
    };
    @PostMapping(PREFIX + "/updateFileByte")
    FileCreateReqDTO newUpdateFileByte(@Valid @RequestBody FileCreateReqDTO createReqDTO);

    default FileCreateReqDTO updateFileRelation(@RequestParam("foreignTableId") Long foreignTableId,@RequestParam("foreignTableName") String foreignTableName, @RequestParam("foreignFileType") String foreignFileType, @RequestParam("name") String name, @RequestParam("path") String path, @RequestParam("content") byte[] content){
        return updateFileRelation(new FileCreateReqDTO().setForeignTableId(foreignTableId).setForeignTableName(foreignTableName).setForeignFileType(foreignFileType).setName(name).setPath(path).setContent(content));
    };
    @PostMapping(PREFIX + "/updateFileRelation")
    FileCreateReqDTO updateFileRelation(@Valid @RequestBody FileCreateReqDTO createReqDTO);

//    default List<FileCreateReqDTO> getListByForeignTableInfoIn(@RequestParam("foreignTableId") Long foreignTableId,@RequestParam("foreignTableName") String foreignTableName, @RequestParam("foreignFileType") String foreignFileType){
//        return getListByForeignTableInfoIn(foreignTableId,foreignTableName,foreignFileType);
//    }

    @PostMapping(PREFIX + "/updateFileRelationPublic")
    List<FileCreateReqDTO> updateFileRelationPublic(@RequestBody FileRelationReqDTO reqDTO);

    @PostMapping(PREFIX + "/updateFileRelationPublic2")
    List<FileCreateReqDTO> updateFileRelationPublic2(@RequestBody FileRelationReqDTO reqDTO);

    @PostMapping(PREFIX + "/getListByForeignTableInfoIn")
    List<FileCreateReqDTO> getListByForeignTableInfoIn(@RequestParam("foreignTableId")Long foreignTableId,@RequestParam("foreignTableName") String foreignTableName, @RequestParam("foreignFileType") String foreignFileType);

    @PostMapping(PREFIX + "/uploadFileFile")
    @Operation(summary = "上传文件")
    @Parameters({
            @Parameter(name = "name", description = "文件名称", example = "文件名称", required = true),
            @Parameter(name = "path", description = "文件路径", example = "/", required = true),
            @Parameter(name = "content", description = "文件字节", example = "[]", required = true)})
    FileCreateReqDTO newCreateFileFile(@RequestParam("name") String name, @RequestParam("path") String path, @RequestParam("content") MultipartFile file);

    @PostMapping(PREFIX + "/copyFileRelationPublic")
    void copyFileRelationPublic(@RequestParam("foreignTableId")Long foreignTableId,@RequestParam("foreignTableName") String foreignTableName,
                                @RequestParam("foreignFileType") String foreignFileType,@RequestParam("oldForeignTableId") Long oldForeignTableId,
                                @RequestParam("oldForeignTableName") String oldForeignTableName,@RequestParam("oldForeignFileType") String oldForeignFileType);

    @PostMapping(PREFIX + "/deleteFileListInUrl")
    void deleteFileListInUrl(@RequestParam("foreignTableId")Long id,@RequestParam("foreignTableName") String saleContinuePay,@RequestParam("foreignFileType") String number);
}
